package room

import (
	"server/common"

	"errors"

	"github.com/mitchellh/mapstructure"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) checkStopProduceReqData(data common.SFUStopProduceReqData) error {
	// Check if room ID is provided
	if data.RoomId == "" {
		return errors.New("room ID is required in stop produce request")
	}

	// Check if user ID is provided
	if data.UserId == "" {
		return errors.New("user ID is required in stop produce request")
	}

	// Check media information
	if err := common.ValidateMediaInfo(data.Media); err != nil {
		return errors.New("invalid media information in stop produce request: " + err.Error())
	}

	// Check if producer ID is provided
	if data.ProducerId == "" {
		return errors.New("producer ID is required in stop produce request")
	}

	// Check if room exists
	if !s.roomMgr.RoomExists(data.RoomId) {
		return errors.New("room does not exist")
	}

	// Check if user exists in the room
	if !s.roomMgr.UserExists(data.RoomId, data.UserId) {
		return errors.New("user does not exist in the room")
	}

	// Check if transport exists
	if !s.roomMgr.SendTransportExists(data.RoomId, data.UserId) {
		return errors.New("transport does not exist in the room")
	}

	return nil
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) sendStopProduceResponse(rid string, code int, msg string, connId string,
	data common.SFUStopProduceReqData) {
	resMsg := common.Message{
		Cmd: common.SFU_STOP_PRODUCE_RES,
		Rid: rid,
		Data: common.ResMsgData{
			Code: code,
			Msg:  msg,
			Data: common.SFUStopProduceResData{
				RoomId:     data.RoomId,
				UserId:     data.UserId,
				Media:      data.Media,
				ProducerId: data.ProducerId,
			},
		},
	}
	s.dispatcher.Dispatch(resMsg, s.serviceInfo, common.AccessServiceInfo, connId)
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func (s *RoomService) handleStopProduceReq(msg common.Message, from common.ServiceInfo,
	connId string) error {
	LOG(msg.Rid).WithField("msg", common.ToJson(msg)).Info("handling stop produce request")

	var reqData common.SFUStopProduceReqData
	if err := mapstructure.Decode(msg.Data, &reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to parse stop produce request")
		s.sendStopProduceResponse(msg.Rid, 1, err.Error(), connId, reqData)
		return nil
	}

	if err := s.checkStopProduceReqData(reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("invalid stop produce request")
		s.sendStopProduceResponse(msg.Rid, 1, err.Error(), connId, reqData)
		return nil
	}

	if err := s.roomMgr.CloseProducer(msg.Rid, reqData); err != nil {
		LOG(msg.Rid).WithError(err).Error("failed to stop produce")
		return nil
	}

	s.sendStopProduceResponse(msg.Rid, 0, "success", connId, reqData)

	LOG(msg.Rid).Info("stop produce request handled successfully")

	return nil
}
